package com.nk.iexam.ds;

import com.nk.iexam.models.Subject;

import android.content.ContentValues;
import android.database.Cursor;

public class SubjectDataSource extends AbstractObjectDataSource<Subject>
{
	public static final int SUBECT_ID_MATH = 1;
	public static final int SUBECT_ID_LITTERATURE = 2;
	public static final int SUBECT_ID_ENGLISH = 3;
	public static final int SUBECT_ID_PHYSICS = 4;
	public static final int SUBECT_ID_CHEMISCAL = 5;
	public static final int SUBECT_ID_BIOLOGY = 6;
	public static final int SUBECT_ID_HISTORY = 7;
	public static final int SUBECT_ID_GEOGRAPHIC = 8;
	
	public static final String TABLE_NAME = "tblSubject";
	public static final String NAME = "Name";
	public static final String DESCRIPTION = "Description";
	
	public SubjectDataSource()
	{
		super();
		_tableName = TABLE_NAME;
		_allColumnsName.add(NAME);
		_allColumnsName.add(DESCRIPTION);
	}
	
	public String getTableName()
	{
		return TABLE_NAME;
	}

	public Subject create(final String name_, final String desc_) 
	{
		ContentValues values = new ContentValues();
		values.put(NAME, name_);
		values.put(DESCRIPTION, desc_);
		
		long insertId = _database.insert(TABLE_NAME, null, values);
		Cursor cursor = _database.query(TABLE_NAME, getAllTableColumnsName(), ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		Subject newSubject = cursorToObject(cursor);
		cursor.close();
		return newSubject;
	}
	
	protected Subject cursorToObject(final Cursor cursor_)
	{
		Subject subject = new Subject();
		subject.setId(cursor_.getLong(0));
		subject.setName(cursor_.getString(1));
		subject.setDescription(cursor_.getString(2));
		
		return subject;
	}	
}
